import { fileURLToPath, URL } from 'node:url'

import {defineConfig, loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  console.log(mode);
  const customEnv = loadEnv(mode, './env', 'VITE_'); // 获取自定义环境变量

  console.log(customEnv.VITE_APP_BASE_URL, customEnv.VITE_APP_BASE_API);

  return {
    envDir: './env',
    plugins: [
      vue(),
      AutoImport({
        imports: ['vue', 'vue-router', 'vue-i18n'],
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver()],
      })
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
        'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js',
      }
    },
    server: {
      open: true,
      hmr: true,
      proxy: {
        [customEnv.VITE_APP_BASE_API]: {
          target: customEnv.VITE_APP_BASE_URL,
          changeOrigin: true,
          rewrite: (path) => path.replace(customEnv.VITE_APP_BASE_API, ''),
        }
      },
      disableHostCheck: true
    },
  }
})
